import {
    BroadcastRequest,
    BroadcastResponse,
    BroadcastResponseStatus,
    BroadcastType,
    Controller,
    ControllerNo,
    GuiControllerOpt,
    GuiId,
    GuiKeys,
    TimeControllerOpt
} from "./model/ControllerOpt";
import {ControllerBroadcast} from "./ControllerBroadcast";
import {SerializeBody} from "./model/SerializeBody";
import GoodsBar from "../../gui/code/goodsBar/GoodsBar";
import ButtonEnter from "../../gui/code/common/ButtonEnter";
import {CombinationUIInfo, ItemShopInfo, MultiBoradRow, RewardType, TaskApplyType} from "../type/Type";
import EquipBar from "../../gui/code/equipBar/EquipBar";
import {ConfigKey, ResumeType} from "../constant/ConfigConstant";
import {TaskController} from "./TaskController";
import {GoodController} from "./GoodController";
import Configer from "../frame/apiModel/config/Configer";
import TaskButton from "../../gui/code/taskBar/TaskButton";
import ChooseHeroBar from "../../gui/code/chooseHeroBar/ChooseHeroBar";
import ThreeChooseOneAchBar from "../../gui/code/chooseAchBar/ThreeChooseOneAchBar";
import {AchievementController} from "./AchievementController";
import {TimeController} from "./TimeController";
import {UnitController} from "./UnitController";
import {GlobalEnv} from "../utils/GlobalEnv";
import ThreeChooseOneGoodBar from "../../gui/code/goodsBar/ThreeChooseOneGoodBar";
import PetBar from "../../gui/code/petBar/PetBar";
import CombinationBar from "../../gui/code/combinationBar/CombinationBar";
import DamageButton from "../../gui/code/damagePanelBar/DamageButton";
import {PlayerModel} from "../frame/apiModel/adapterModel/model4/PlayerModel";
import {PlayerFactory} from "../frame/PlayerFactory";
import * as React from "../frame/apiModel/gui/React";
import {ComponentTree, SyncId, UISyncData, View} from "../frame/apiModel/gui/React";
import {SyncDataKey, TriggerFactory, TriggerKey} from "../frame/TriggerFactory";
import {GlobalModel} from "../frame/apiModel/adapterModel/model1/GlobalModel";
import {UIConfigKey} from "../constant/UIConfig";
import {ModelFactory} from "../frame/apiModel/adapterModel/model5/ModelFactory";
import {UnitModel} from "../frame/apiModel/adapterModel/model3/UnitModel";
import {DamagePanelType} from "../../gui/code/damagePanelBar/DamagePanelBar";
import ResumeBar from "../../gui/code/resumeBar/ResumeBar";
import {CodeUtil} from "../frame/codeAdapter/CodeUtil";
import {ItemDataKey, PlayerDataKey, UnitDataKey} from "../frame/dataSource/DataSource";
import BossLifeBar from "../../gui/code/common/BossLifeBar";
import ChooseDiffBar from "../../gui/code/chooseDiffBar/ChooseDiffBar";
import {FinalKey} from "../constant/FinalConstant";
import AchievementBar from "../../gui/code/achievementBar/AchievementBar";
import AttrBar from "../../gui/code/attrBar/AttrBar";
import HandBookBar from "../../gui/code/handBookBar/HandBookBar";
import AppearanceBar from "../../gui/code/appearanceBar/AppearanceBar";
import UnitUtil from "../utils/UnitUtil";
import IntegralMallBar from "../../gui/code/IntegralShopBar/IntegralShopBar";
import TextGuiBar from "../../gui/code/textBar/TextGuiBar";
import ToastWheelRowBar from "../../gui/code/textBar/ToastWheelColBar";
import HideAchievementBar from "../../gui/code/hideAchievementBar/HideAchievementBar";
import {InputUtil} from "../utils/InputUtil";
import {
    AchBarInterface,
    AdminBarInterface,
    AppearanceBarInterface,
    AttrBarInterface,
    AvatarBarInterface,
    AvatarInfo,
    BagInfo,
    BossLifeBarInterface,
    BuffBarInfo,
    BuffBarInterface,
    ButtonEnterInterface,
    ChatBarInterface,
    ChatInfo,
    ChooseArchiveBarInterface,
    ChooseDiffBarInterface,
    ChooseHeroBarInterface,
    CombinationBarInterface,
    DamageButtonInterface,
    EquipBarInterface,
    EquipInfo,
    GoodsBarInterface,
    HandBookBarInterface,
    HideAchievementBarInterface,
    IntegralMallBarInterface,
    ItemBarInterface,
    ItemInfo,
    MultiBoradBarInterface,
    PetBarInterface,
    PetInfo,
    ResumeBarInterface,
    SkillBarInterface,
    SmallAttrBarInterface,
    TalentBarInterface,
    TargetItemInfo,
    TaskButtonInterface,
    TextGuiBarInterface,
    ThreeChooseOneGoodRootInterface,
    ToastWheelBarInterface,
    TopBarInfo,
    TopBarInterface,
    UnitShopInterface,
} from "../../gui/interface/GuiInterface";
import {ResourceKey} from "../constant/ResourceConstant";
import {ModelType} from "../frame/apiModel/adapter/adapterInterface/model5/IModelFactory";
import {frameAdapter} from "../frame/apiModel/adapter/adapterInterface/model1/IFrameAdapter";
import MultiBoradBar from "parasimon/gui/code/multiBoradBar/MultiBoradBar";
import SmallAttrBar from "../../gui/code/smallAttrBar/SmallAttrBar";
import AvatarBar from "../../gui/code/smallAttrBar/AvatarBar";
import SkillBar from "../../gui/code/smallAttrBar/SkillBar";
import ItemBar from "parasimon/gui/code/smallAttrBar/ItemBar";
import TopBar from "parasimon/gui/code/topBar/TopBar";
import {AbilityController} from "./AbilityController";
import {AttributeEnum, CastType1, UnitAttribute} from "../frame/dataSource/DataModel";
import UnitShopBar from "../../gui/code/unitShopBar/UnitShopBar";
import {ItemModel} from "../frame/apiModel/adapterModel/model3/ItemModel";
import ChatBar from "parasimon/gui/code/textBar/ChatBar";
import {DataRouter} from "../frame/dataSource/DataRouter";
import {Logger} from "../frame/apiModel/config/Logger";
import MathUtil from "../frame/codeAdapter/MathUtil";
import BuffBar from "parasimon/gui/code/textBar/BuffBar";
import TalentBarPage from "../../gui/code/talentBar/TalentBarPage";
import ChooseArchiveBar from "parasimon/gui/code/chooseArchiveBar/ChooseArchiveBar";
import {PageContentType} from "../../gui/code/component/PageContent";
import {publicGui} from "../../gui/interface/PublicGui";
import {UnitJson} from "../frame/apiModel/model/StoreJson";


export enum OptType {
    open = 1,
    close,
    reverse,
    reflush,
}

export class GuiSourceMap {
    [GuiId.goodsBar]: GoodsBarInterface<any, any>;
    [GuiId.equipBar]: EquipBarInterface<any, any>;

    [GuiId.buttonEnter]: ButtonEnterInterface<any, any>;

    [GuiId.textGuiBar]: TextGuiBarInterface<any, any>;
    [GuiId.toastWheelBar]: ToastWheelBarInterface<any, any>;

    [GuiId.taskButton]: TaskButtonInterface<any, any>;

    [GuiId.chooseHeroBar]: ChooseHeroBarInterface<any, any>;

    [GuiId.chooseAchBar]: ThreeChooseOneGoodRootInterface<any, any>;
    [GuiId.chooseItemBar]: ThreeChooseOneGoodRootInterface<any, any>;
    [GuiId.damageButton]: DamageButtonInterface<any, any>;
    [GuiId.combinationBar]: CombinationBarInterface<any, any>;
    [GuiId.petBar]: PetBarInterface<any, any>;
    [GuiId.resumeBar]: ResumeBarInterface<any, any>;
    [GuiId.bossLifeBar]: BossLifeBarInterface<any, any>;
    [GuiId.chooseDiffBar]: ChooseDiffBarInterface<any, any>;

    [GuiId.achBar]: AchBarInterface<any, any>;
    [GuiId.hideAchBar]: HideAchievementBarInterface<any, any>;
    [GuiId.talentBar]: TalentBarInterface<any, any>;
    [GuiId.attrBar]: AttrBarInterface<any, any>;
    [GuiId.handBookBar]: HandBookBarInterface<any, any>;
    [GuiId.appBar]: AppearanceBarInterface<any, any>;
    [GuiId.integralMallBar]: IntegralMallBarInterface<any, any>;

    [GuiId.buffBar]: BuffBarInterface<any, any>;
    [GuiId.multiBoradBar]: MultiBoradBarInterface<any, any>;
    [GuiId.smallAttrBar]: SmallAttrBarInterface<any, any>;
    [GuiId.avatarBar]: AvatarBarInterface<any, any>;
    [GuiId.skillBar]: SkillBarInterface<any, any>;
    [GuiId.itemBar]: ItemBarInterface<any, any>;
    [GuiId.unitShopBar]: UnitShopInterface<any, any>;
    [GuiId.chooseArchiveBar]: ChooseArchiveBarInterface<any, any>;

    [GuiId.adminBar]: AdminBarInterface<any, any>;


    getDamageButton(): DamageButtonInterface<any, any> {
        const getHurts = (type: DamagePanelType, player: PlayerModel): { name: string, value: number, id: number }[] => {
            switch (type) {
                case DamagePanelType.players:{
                    return GlobalEnv.getPlayerHurts();
                }
                case DamagePanelType.pets:{
                    return UnitController.getInstance().getUnitHurts(player);
                }
            }
        }
        return <DamageButton  key={GuiKeys.damageButton} getHurts={getHurts} />;
    }
    getGoodsBar(): GoodsBarInterface<any, any> {
        const getBagsFun = (player: PlayerModel): BagInfo => {
            const hero = GlobalEnv.getHero(player);
            if (hero?.isValid()) {
                const lv = hero.level;

                const goodsBags = player.data[PlayerDataKey.goodsBag];

                const bagArr: ItemInfo[] = [];
                const maxGoodsBagNum = Configer.getConfig(ConfigKey.maxGoodsBagNum);
                for (let i = 0; i < maxGoodsBagNum; i++) {
                    const good = goodsBags[i];
                    if (good?.isValid()) {
                        bagArr.push({ strId: good.strId, num: good.charges, affixs: good.data[ItemDataKey.itemAffix], lv: good.data[ItemDataKey.forgLv] })
                    } else {
                        bagArr.push({} as any)
                    }
                }

                const equips = hero.data[UnitDataKey.unitEquip];
                const equipArr: ItemInfo[] = [];
                const maxEquipNum = Configer.getConfig(ConfigKey.maxGoodsEquipNum);
                for (let i = 0; i < maxEquipNum; i++) {
                    const equip = equips[i];
                    if (equip?.isValid()) {
                        equipArr.push({ strId: equip.strId, num: equip.charges, affixs: equip.data[ItemDataKey.itemAffix], lv: equip.data[ItemDataKey.forgLv] })
                    } else {
                        equipArr.push({} as any)
                    }
                }
                return  { lv, bags: bagArr, equips: equipArr }
            }
            return  { lv: 0, bags: [], equips: [] };
        }
        const getCombinationInfoFun = (player: PlayerModel, itemId: string) => {
            const hero = GlobalEnv.getHero(player);
            const combinationInfo: CombinationUIInfo = GoodController.getInstance().getAllGoodCombinationBodyByPlayer(hero, itemId);
            return combinationInfo
        }
        return <GoodsBar key={GuiKeys.goodBar} getBags={getBagsFun} getCombinationInfoFun={getCombinationInfoFun}></GoodsBar>
    };
    getEquipBar(): EquipBarInterface<any, any> {
        const getEquipFun = (player: PlayerModel): EquipInfo => {
            // const selectUnit = player.data[PlayerDataKey.selectedUnit];
            const unit = GlobalEnv.getHero(player)
            if (unit?.isValid()) {
                const lv = unit.level;
                let isOwner = false;
                const equips = unit.data[UnitDataKey.unitEquip];
                const ret: ItemInfo[] = [];
                const maxEquipNum = Configer.getConfig(ConfigKey.maxGoodsEquipNum);
                for (let i = 0; i < maxEquipNum; i++) {
                    const equip = equips[i];
                    if (equip?.isValid()) {
                        ret.push({ strId: equip.strId, num: equip.charges, affixs: equip.data[ItemDataKey.itemAffix], lv: equip.data[ItemDataKey.forgLv] })
                    } else {
                        ret.push({} as any)
                    }
                }

                const localPlayer = PlayerFactory.getInstance().getLocalPlayer();
                if (localPlayer === player) {
                    isOwner = true;
                }

                return { lv, items: ret, isOwner };
            }
            return { lv: 0, items: [], isOwner: false };
        }
        const getCombinationInfoFun = (player: PlayerModel, itemId: string) => {
            const hero = GlobalEnv.getHero(player);
            const combinationInfo: CombinationUIInfo = GoodController.getInstance().getAllGoodCombinationBodyByPlayer(hero, itemId);
            return combinationInfo
        }
        return <EquipBar key={GuiKeys.equipBar} getEquip={getEquipFun} getCombinationInfoFun={getCombinationInfoFun}/>;
    };
    getButtonEnter(): ButtonEnterInterface<any, any> {
        return <ButtonEnter    key={GuiKeys.buttonEnter} />
    };
    getTextGuiBar(): TextGuiBarInterface<any, any>{
        return <TextGuiBar    key={GuiKeys.textGui} />
    };
    getToastWheelBar(): ToastWheelBarInterface<any, any> {
        const getToastsFun = (): ChatInfo => {
            const chats = GlobalModel.toasts;
            const chatsIndex = GlobalModel.toastsIndex;
            return { chats, chatsIndex };
        }
        return <ToastWheelRowBar getToasts={getToastsFun} key={GuiKeys.wheelGui} />
    };
    getTaskButton(): TaskButtonInterface<any, any> {
        const getTypeTaskRet = (player: PlayerModel) => {
            const typeTaskRet = TaskController.getInstance().getDoingTask(player);
            return typeTaskRet;
        }
        const getApplyTaskFun = (player: PlayerModel) => {
            const unitApplyTask = player.data[PlayerDataKey.unitApplyTask];
            return unitApplyTask;
        }
        return <TaskButton    key={GuiKeys.taskButtonCon} getApplyTaskFun={getApplyTaskFun} getTaskFun={getTypeTaskRet}></TaskButton>
    };
    getChooseHeroBar(): ChooseHeroBarInterface<any, any>{
        return <ChooseHeroBar    key={GuiKeys.chooseHeroBarCon} />
    };
    getChooseArchiveBar(): ChooseArchiveBarInterface<any, any> {
        return <ChooseArchiveBar   key={GuiKeys.chooseArchiveBarCon} />
    };
    getChooseAchBar(): ThreeChooseOneGoodRootInterface<any, any> {
        const getAchModelFun = (player: PlayerModel) => {
            const threeChooseOneModels = AchievementController.getInstance().getChooseTalentPoolData(player);
            return threeChooseOneModels;
        }
        return <ThreeChooseOneAchBar    key={GuiKeys.threeChooseOneAchBar} getChooseItem={getAchModelFun}></ThreeChooseOneAchBar>;
    };
    getChooseItemBar(): ThreeChooseOneGoodRootInterface<any, any>{
        const getItemModelFun = (player: PlayerModel, id: string) => {
            const hero = GlobalEnv.getHero(player);
            if (hero?.isValid()) {
                const threeChooseOneModels = GoodController.getInstance().getUiChooseItem(hero, id);
                return threeChooseOneModels;
            }

        }
        return <ThreeChooseOneGoodBar    key={GuiKeys.threeChooseOneGoodBar} getChooseItem={getItemModelFun}></ThreeChooseOneGoodBar>;
    };
    getChatBar(): ChatBarInterface<any, any>{
        const getChatsFun = (): ChatInfo => {
            const chats = GlobalModel.chats;
            const chatsIndex = GlobalModel.chatsIndex;
            return { chats, chatsIndex };
        }
        return <ChatBar key={GuiKeys.chatBarCon} getChats={getChatsFun} />
    }
    getCombinationBar(): CombinationBarInterface<any, any>{
        const getCombinationInfoFun = (player: PlayerModel) => {
            const combinationInfo: CombinationUIInfo = UnitController.getInstance().getAllUnitCombinationBodyByPlayer(player);
            return combinationInfo
        }
        return <CombinationBar    key={GuiKeys.combinationBar} getCombinationInfoFun={getCombinationInfoFun}/>;
    };
    getPetBar(): PetBarInterface<any, any> {
        const getPetInfoFun = (player: PlayerModel) => {
            const ret: PetInfo[] = [];
            const petsInfo: UnitJson[] = UnitController.getInstance().getPetInfo(player);
            const pets: UnitModel[] = UnitController.getInstance().getPets(player);
            const maxPetNum = Configer.getConfig(ConfigKey.maxPetNum);
            for (let i = 0; i < maxPetNum; i++) {
                const pet = pets[i];
                const petInfo = petsInfo[i];
                let info: PetInfo;
                if (pet?.isValid && pet?.isValid()) {
                    info = {
                        attr: pet.data[UnitDataKey.newestAttribute],
                        bAlive: pet.isAlive(),
                        bBattle: (pet.data[UnitDataKey.bBattle] > 0),
                        bVaild: true,
                        exp: petInfo.e,
                        id: petInfo.id,
                        lv: pet.level,
                        resumeTime: pet.data[UnitDataKey.resumeTime]
                    }
                } else {
                    info = {
                        attr: new UnitAttribute(),
                        bAlive: false,
                        bBattle: false,
                        bVaild: false,
                        exp: petInfo?.e,
                        id: petInfo?.id,
                        lv: 0,
                        resumeTime: 0
                    }
                }
                ret.push(info);
            }
            return ret;
        }
        return <PetBar key={GuiKeys.petBarCon} getPetInfoFun={getPetInfoFun}></PetBar>;
    };
    getResumeBar(): ResumeBarInterface<any, any> {
        const resumeTypes: ResumeType[] = Configer.getConfig(ConfigKey.resumeTypes);
        const isUIResume = CodeUtil.inArr(resumeTypes, ResumeType.UIResume);
        if (isUIResume) {
            return <ResumeBar    key={GuiKeys.resumeBar}></ResumeBar>
        }
    };
    getBossLifeBar(): BossLifeBarInterface<any, any> {
        return <BossLifeBar    key={GuiKeys.bossLifeBarContent} />
    };
    getChooseDiffBar(): ChooseDiffBarInterface<any, any> {
        return <ChooseDiffBar    key={GuiKeys.chooseDiffBar} />;
    };
    getAchBar(): AchBarInterface<any, any> {
        const getAchFun = (player: PlayerModel) => {
            return AchievementController.getInstance().getAchievement(player)
        }
       return <AchievementBar    key={GuiKeys.achBar} getAchFun={getAchFun} pageContentType={PageContentType.circle} />
    };
    getHideAchBar(): HideAchievementBarInterface<any, any> {
        const getAchFun = (player: PlayerModel) => {
            return AchievementController.getInstance().getAchievement(player)
        }
        return <HideAchievementBar    key={GuiKeys.hideAchBar} getAchFun={getAchFun} pageContentType={PageContentType.circle} />
    };
    getTalentBar(): TalentBarInterface<any, any> {
        const getAchFun = (player: PlayerModel) => {
            return AchievementController.getInstance().getAchievement(player)
        }
        return <TalentBarPage    key={GuiKeys.talentBar} getAchFun={getAchFun} pageContentType={PageContentType.normal} />
    };
    getAttrBar(): AttrBarInterface<any, any> {
        const getAttrFun = (player: PlayerModel) => {
            const hero = GlobalEnv.getHero(player);
            const ret = UnitUtil.getAttribute(hero);
            return ret.newestAttribute
        }
        return <AttrBar    key={GuiKeys.attrBar} getAttrFun={getAttrFun} />
    };
    getHandBookBar(): HandBookBarInterface<any, any> {
        const getHandBookFun = (player: PlayerModel) => {
            return  AchievementController.getInstance().getHandBook(player);
        }
        const getHandBookExpFun = (player: PlayerModel) => {
            return  AchievementController.getInstance().getExp(player);
        }
        return <HandBookBar    key={GuiKeys.handBookBar} getHandBookFun={getHandBookFun} getHandBookExpFun={getHandBookExpFun}  pageContentType={PageContentType.circle}/>
    };
    getAppBar(): AppearanceBarInterface<any, any> {
        const getAchFun = (player: PlayerModel) => {
            return AchievementController.getInstance().getAchievement(player)
        }
        return <AppearanceBar   key={GuiKeys.appearBar} getAchFun={getAchFun} pageContentType={PageContentType.normal}></AppearanceBar>
    };
    getIntegralMallBar(): IntegralMallBarInterface<any, any> {
        const getAchFun = (player: PlayerModel) => {
            return AchievementController.getInstance().getAchievement(player)
        }
        const getIntegralFun = (player: PlayerModel) => {
            return AchievementController.getInstance().getIntegral(player)
        }
        return <IntegralMallBar    key={GuiKeys.integralMallBar} getAchFun={getAchFun} getIntegralFun={getIntegralFun}  pageContentType={PageContentType.circle}></IntegralMallBar>
    };
    getBuffBar(): BuffBarInterface<any, any> {
        const getBuffFun = (player: PlayerModel): BuffBarInfo => {
            const unit = player.data[PlayerDataKey.selectedUnit]
            if (unit) {
                const buffs = AbilityController.getInstance().getBuffs(unit);
                const attr = UnitUtil.getAttribute(unit);
                const newestAttr = attr.newestAttribute;

                return { buffs, attr: newestAttr, unit: unit }
            }
        }
        return <BuffBar key={GuiKeys.buffBarContent} getBuffFun={getBuffFun}></BuffBar>
    };
    getMultiBoradBar(): MultiBoradBarInterface<any, any> {
        const getRows = (): { rows: MultiBoradRow[], title: string } => {
            const rows: MultiBoradRow[] = [{
                userName:  '玩家名',
                // heroName:  '英雄名',
                heroLevel: '等级',
                fightValue: '战斗力',
                // integral: '积分',
            }];
            PlayerFactory.getInstance().mapAllPlayer((player: PlayerModel) => {
                const row = GuiController.getInstance().getMultiBoradRow(player);
                rows.push(row);
            })
            const title = '排行榜';
            return { rows, title };
        }
        return <MultiBoradBar    key={GuiKeys.multiBarCon} getRows={getRows} ></MultiBoradBar>;
    };
    getSmallAttrBar(): SmallAttrBarInterface<any, any> {
        const getSelectedAttrFun = (player: PlayerModel) => {
            let selectedUnit = player.getLastSelectedUnit()
            if (selectedUnit?.isAliveInBattle()) {
            } else {
                selectedUnit = undefined;
            }
            if (selectedUnit?.isAlive()) {
                const attr = UnitUtil.getAttribute(selectedUnit);
                return attr.newestAttribute
            }
            return undefined
        }
        return <SmallAttrBar    key={GuiKeys.smallAttrBarCon} getAttr={getSelectedAttrFun} />
    };
    getAvatarBar(): AvatarBarInterface<any, any> {
        const getAvatarInfoFun = (player: PlayerModel) => {
            let selectedUnit = player.getLastSelectedUnit()
            if (selectedUnit?.isAliveInBattle()) {
            } else {
                selectedUnit = undefined;
            }
            if (selectedUnit?.isValid()) {
                const icon = selectedUnit?.getIconPath();
                const life = selectedUnit?.life;
                const maxLife = selectedUnit?.maxLife;
                const mana = selectedUnit?.mana;
                const maxMana = selectedUnit?.maxMana;
                const lv = selectedUnit?.level;
                const exp = selectedUnit?.experience;
                const name = selectedUnit?.name;
                const resumeTime = selectedUnit?.data[UnitDataKey.resumeTime] ? selectedUnit?.data[UnitDataKey.resumeTime] : 0;
                const maxResumeTime = selectedUnit?.template?.resumeTime ? selectedUnit?.template?.resumeTime : 0;
                const avatarInfo: AvatarInfo = { icon, life, maxLife, mana, maxMana, lv, exp, name, resumeTime: resumeTime, maxResumeTime }
                return avatarInfo
            }
            return { icon: "", life: 0, maxLife: 0, mana: 0, maxMana: 0, lv: 0, exp: 0, name: "", resumeTime: 0, maxResumeTime: 0 };
        }

        return <AvatarBar  key={GuiKeys.heroAvatarBarCon} getAvatarInfo={getAvatarInfoFun} />
    };
    getSkillBar(): SkillBarInterface<any, any> {
        const getAbilitysFun = (player: PlayerModel) => {
            const selectedUnit = player.getLastSelectedUnit();
            if (selectedUnit?.isValid()) {
                const attr = UnitUtil.getAttribute(selectedUnit);
                const newestAttribute = attr.newestAttribute;
                const cr = newestAttribute[AttributeEnum.coolingReduction];
                const magicValue = selectedUnit.mana;
                const abilitys = AbilityController.getInstance().getAbilitys(selectedUnit);
                const achs = AchievementController.getInstance().getAchievement(player);
                return { abilitys, cr, achs, magicValue }
            }
            return { abilitys: {}, achs: {}, cr: 0, magicValue: 0 }
        }
        return <SkillBar    key={GuiKeys.skillBarCon}  getAbilitysFun={getAbilitysFun}/>
    };
    getItemBar(): ItemBarInterface<any, any> {
        const getItemsFun = (player: PlayerModel): { items: ItemModel[], targetItemInfo: TargetItemInfo } => {
            const selectedUnit = player.getLastSelectedUnit();
            if (selectedUnit?.isValid()) {
                const itemIndex = GoodController.getInstance().getItemBagIndex(selectedUnit);
                const itemJsons = GoodController.getInstance().getItemBagByIndex(selectedUnit, itemIndex);


                const player = PlayerFactory.getInstance().getUnitOwner(selectedUnit);
                const targetItem: ItemModel = player.data[PlayerDataKey.targetItem];
                let targetItemInfo: TargetItemInfo = { id: '', x: 0, y: 0, handleId: '' }
                if (targetItem) {
                    targetItemInfo.id = targetItem.strId;
                    targetItemInfo.x = targetItem.data[ItemDataKey.fakeX];
                    targetItemInfo.y = targetItem.data[ItemDataKey.fakeY];
                    targetItemInfo.handleId = targetItem.id
                }
                return { items: itemJsons, targetItemInfo: targetItemInfo }
            }
            return { items: [], targetItemInfo: { id: '', x: 0, y: 0, handleId: '' } }
        }
        return <ItemBar    key={GuiKeys.itemBarCon} getItemsFun={getItemsFun} />
    };
    getUnitShopBar(): UnitShopInterface<any, any> {
        const getItemShopFun = (player: PlayerModel): { items: ItemShopInfo[], sellUnitId: string, buyUnitId: string } => {
            const selectedUnit = player.data[PlayerDataKey.selectedUnit];
            if (selectedUnit?.isValid()) {
                const sellRange = Configer.getConfig(ConfigKey.sellItemRange);
                const items = GuiController.getInstance().getItemShopInfos(selectedUnit, player);
                let buyUnitId = '';


                const buyUnit = player.getLastSelectedUnit();
                if (buyUnit?.isValid()) {
                    if (buyUnit.loc.getAbsDistance(selectedUnit.loc) < sellRange) {
                        buyUnitId = buyUnit.getHandleId()
                    }
                }
                return { items, sellUnitId: selectedUnit.getHandleId(), buyUnitId: buyUnitId }
            }
            return { items: [], sellUnitId: '', buyUnitId: '' }
        }
        return <UnitShopBar    key={GuiKeys.itemShopBarCon} getItemsFun={getItemShopFun} pageContentType={PageContentType.circle} />
    };
    getTopBar(): TopBarInterface<any, any> {
        const getTopInfoFun = (player: PlayerModel) => {
            const coin = player.gold;
            const wood = player.lumber;
            const killNum = GlobalEnv.getKillNum(player);
            const monsterNum = GlobalEnv.getAllAttackMonsterNum();

            const maxMonsterNum = GlobalEnv.getAllMaxAttackMonsterNum();

            const wave = GlobalEnv.wave;
            let waveTime;
            const tempTime = GlobalEnv.waveTimer?.timer?.getRemaining()
            if (tempTime) {
                waveTime = MathUtil.floor(tempTime);
            }
            let diffNum = 0;
            if (GlobalEnv.difficult) {
                diffNum = GlobalEnv.difficult + 1;
            }
            const topInfo: TopBarInfo = { coin, wood, killNum, monsterNum, maxMonsterNum, wave, waveTime, diffNum };
            return  topInfo;
        }
        return <TopBar    key={GuiKeys.topBarCon} getTopInfo={getTopInfoFun} />
    };
    getAdminBar(): AdminBarInterface<any, any> {
        return undefined;
        // return <AdminBar  key={GuiKeys.adminBarCon} />;
    };

    initDefault() {
        frameAdapter.adapter.initDefaultFrame(true);
    }

    initFun = () => {
        this[GuiId.damageButton] = this.getDamageButton()
        React.render(this[GuiId.damageButton], ComponentTree.getInstance().root)

        this[GuiId.goodsBar] = this.getGoodsBar();
        React.render(this[GuiId.goodsBar], ComponentTree.getInstance().root)

        this[GuiId.equipBar] = this.getEquipBar();
        React.render(this[GuiId.equipBar], ComponentTree.getInstance().root)

        this[GuiId.taskButton] = this.getTaskButton()
        React.render(this[GuiId.taskButton], ComponentTree.getInstance().root)

        this.textGuiBar = this.getTextGuiBar();
        React.render(this[GuiId.textGuiBar], ComponentTree.getInstance().root)

        this[GuiId.bossLifeBar] = this.getBossLifeBar()
        React.render(this[GuiId.bossLifeBar], ComponentTree.getInstance().root)

        this[GuiId.adminBar] = this.getAdminBar();
        React.render(this[GuiId.adminBar], ComponentTree.getInstance().root)


        this[GuiId.topBar] = this.getTopBar();
        React.render(this[GuiId.topBar], ComponentTree.getInstance().root)

        this[GuiId.avatarBar] = this.getAvatarBar();
        React.render(this[GuiId.avatarBar], ComponentTree.getInstance().root)

        this[GuiId.smallAttrBar] = this.getSmallAttrBar();
        React.render(this[GuiId.smallAttrBar], ComponentTree.getInstance().root)

        this[GuiId.skillBar] = this.getSkillBar();
        React.render(this[GuiId.skillBar], ComponentTree.getInstance().root)

        this[GuiId.itemBar] = this.getItemBar();
        React.render(this[GuiId.itemBar], ComponentTree.getInstance().root)

        this[GuiId.unitShopBar] = this.getUnitShopBar();
        React.render(this[GuiId.unitShopBar], ComponentTree.getInstance().root)

        this[GuiId.buttonEnter] = this.getButtonEnter();
        React.render(this[GuiId.buttonEnter], ComponentTree.getInstance().root)

        this[GuiId.achBar] = this.getAchBar();
        React.render(this[GuiId.achBar], ComponentTree.getInstance().root)

        this[GuiId.hideAchBar] = this.getHideAchBar();
        React.render(this[GuiId.hideAchBar], ComponentTree.getInstance().root)

        this[GuiId.talentBar] = this.getTalentBar();
        React.render(this[GuiId.talentBar], ComponentTree.getInstance().root)

        this[GuiId.attrBar] = this.getAttrBar();
        React.render(this[GuiId.attrBar], ComponentTree.getInstance().root)

        this[GuiId.handBookBar] = this.getHandBookBar()
        React.render(this[GuiId.handBookBar], ComponentTree.getInstance().root)

        this[GuiId.appBar] = this.getAppBar()
        React.render(this[GuiId.appBar], ComponentTree.getInstance().root)

        this[GuiId.integralMallBar] = this.getIntegralMallBar()
        React.render(this[GuiId.integralMallBar], ComponentTree.getInstance().root)


        this[GuiId.buffBar] = this.getBuffBar()
        React.render(this[GuiId.buffBar])


        this[GuiId.chooseAchBar] = this.getChooseAchBar()
        React.render(this[GuiId.chooseAchBar], ComponentTree.getInstance().root)

        this[GuiId.chooseItemBar] = this.getChooseItemBar();
        React.render(this[GuiId.chooseItemBar], ComponentTree.getInstance().root)


        this.chooseHeroBar = this.getChooseHeroBar();
        React.render(this[GuiId.chooseHeroBar], ComponentTree.getInstance().root)

        this[GuiId.chooseArchiveBar] = this.getChooseArchiveBar();
        React.render(this[GuiId.chooseArchiveBar], ComponentTree.getInstance().root)

        this[GuiId.petBar] = this.getPetBar()
        React.render(this[GuiId.petBar], ComponentTree.getInstance().root)

        this[GuiId.multiBoradBar] = this.getMultiBoradBar()
        React.render(this[GuiId.multiBoradBar], ComponentTree.getInstance().root)


        this[GuiId.combinationBar] = this.getCombinationBar()
        React.render(this[GuiId.combinationBar], ComponentTree.getInstance().root)


        this.resumeBar = this.getResumeBar()
        React.render(this[GuiId.resumeBar], ComponentTree.getInstance().root)

        this[GuiId.chooseDiffBar] = this.getChooseDiffBar()
        React.render(this[GuiId.chooseDiffBar], ComponentTree.getInstance().root)

        this[GuiId.chatBar] = this.getChatBar()
        React.render(this[GuiId.chatBar], ComponentTree.getInstance().root)

        this[GuiId.toastWheelBar] = this.getToastWheelBar()
        React.render(this[GuiId.toastWheelBar], ComponentTree.getInstance().root)

        publicGui.init();

        this.initDefault();
    }
}

export class GuiController implements Controller {
    getControllerNo(): number {
        return ControllerNo.GuiController;
    }
    private static guiController: GuiController = new GuiController();

    static chooseHeroFun: (player: PlayerModel, id: string) => void;
    static chooseDiffFun: (difficult: number) => void;
    static chooseArchiveFun: (player: PlayerModel) => void;

    guiSourceMap: GuiSourceMap

    static getInstance(): GuiController {
        return GuiController.guiController;
    }
    init = (initAdapter: GuiSourceMap) => {
        this.guiSourceMap = initAdapter;
        ControllerBroadcast.getInstance().startListen(GuiController.getInstance());

        const foreverPeriodic = Configer.getFinal(FinalKey.foreverPeriodic) as number;
        const ReactClearFun = function () {
            ComponentTree.getInstance().clear();
        }
        const ReactClearOpt = new TimeControllerOpt(undefined, ReactClearFun, foreverPeriodic);
        TimeController.getInstance().startTimer({time: 0.05, opt: ReactClearOpt});

        ComponentTree.getInstance().root = <View key={GuiKeys.root} style={{ width: 1, height:1 }}></View>


        TriggerFactory.getInstance().registerSyncData(SyncDataKey.uiEvent,  function (this: void, triggerPlayer, data: UISyncData) {
            const eventId = data.id;
            const param = data.param;
            switch (eventId) {
                // 关闭按钮需要同步触发异步关闭
                case SyncId.onlyOneOpen: {
                    const guiId: GuiId = param.guiId;
                    GuiController.getInstance().escOtherFun(triggerPlayer, guiId)
                    GuiController.getInstance().showGui(new GuiControllerOpt(guiId, {}), triggerPlayer, OptType.reverse);
                    break;
                }
                case SyncId.closeUI: {
                    const guiId: string = param.guiId;
                    GuiController.getInstance().showGui(new GuiControllerOpt(guiId, {}), triggerPlayer, OptType.close);
                    break;
                }
                case SyncId.chooseHero: {
                    const heroId = param.heroId;
                    GuiController.chooseHeroFun(triggerPlayer, heroId);
                    GuiController.getInstance().showGui(new GuiControllerOpt(GuiId.chooseHeroBar, {}), triggerPlayer, OptType.close);
                    break;
                }
                case SyncId.useAppearance: {
                    const appId = param.appId;
                    const type = param.type;
                    AchievementController.getInstance().useAppearance(triggerPlayer, appId, type)
                    break;
                }
                case SyncId.loseTask: {
                    const taskId = param.taskId;
                    TaskController.getInstance().loseTask(triggerPlayer, taskId);
                    break;
                }
                case SyncId.toastTask: {
                    const taskId = param.taskId;
                    TaskController.getInstance().applyTask(triggerPlayer, taskId, TaskApplyType.onlyFinsh)
                    break;
                }
                case SyncId.callBack: {
                    const index = param.selectIndex;
                    const pet = UnitController.getInstance().getPetByIndex(triggerPlayer, index);
                    if (pet) {
                        const bBattle = pet.data[UnitDataKey.bBattle];
                        if (bBattle > 0) {
                            UnitController.getInstance().callBack(pet, triggerPlayer);
                        } else {
                            const hero = GlobalEnv.getHero(triggerPlayer);
                            UnitController.getInstance().callPet(hero, index);
                        }
                        const opt = new GuiControllerOpt(GuiId.petBar, { });
                        GuiController.getInstance().showGui(opt, triggerPlayer, OptType.reflush);
                    }
                    break;
                }
                case SyncId.release: {
                    const index = param.selectIndex;
                    UnitController.getInstance().releasePet(triggerPlayer, index);
                    const opt = new GuiControllerOpt(GuiId.petBar, { });
                    GuiController.getInstance().showGui(opt, triggerPlayer, OptType.reflush);
                    break;
                }
                case SyncId.chooseItem: {
                    const itemId = param.itemId;
                    const ownerId = param.ownerId;
                    const unit = ModelFactory.getInstance().getModel(ownerId, ModelType.unit) as UnitModel
                    TaskController.getInstance().rewardItem(unit, RewardType.bag, itemId, 0);
                    GuiController.getInstance().showGui(new GuiControllerOpt(GuiId.chooseItemBar, {}), triggerPlayer, OptType.close);
                    break;
                }
                case SyncId.chooseAch: {
                    const achId = param.achId;
                    const reward = param.reward;
                    AchievementController.getInstance().addTalent(triggerPlayer, achId, reward);
                    GuiController.getInstance().showGui(new GuiControllerOpt(GuiId.chooseAchBar, {}), triggerPlayer, OptType.close);
                    break;
                }
                case SyncId.resumeHero: {
                    const cost = param.cost;
                    const ret = triggerPlayer.cosumeGold(cost);
                    if (ret) {
                        UnitController.getInstance().resumeHero(triggerPlayer)
                        UnitController.getInstance().addHeroResumeNum(triggerPlayer);
                        GuiController.getInstance().showGui(new GuiControllerOpt(GuiId.resumeBar, {}), triggerPlayer, OptType.close);
                    }
                    break;
                }
                case SyncId.selectPet: {
                    const index = param.selectIndex;
                    const pet = UnitController.getInstance().getPetByIndex(triggerPlayer, index);
                    if (pet?.isAliveInBattle()) {
                        // pet.select(true);
                        triggerPlayer.moveCamera(pet.loc, 0)
                    }
                    break;
                }
                case SyncId.selectHero: {
                    const index = param.selectIndex;
                    const player = PlayerFactory.getInstance().getPlayer(index);
                    const hero = GlobalEnv.getHero(player);
                    if (hero?.isAliveInBattle()) {
                        // hero.select(true);
                        triggerPlayer.moveCamera(hero.loc, 0)
                    }
                    break;
                }
                case SyncId.chooseDiff: {
                    const difficult = param.difficult;
                    const mode = param.mode;
                    if (difficult && difficult >= 0) {
                        GuiController.chooseDiffFun(difficult);
                    }
                    PlayerFactory.getInstance().mapAllPlayer(
                        (tempPlayer) => {
                            GuiController.getInstance().showGui(new GuiControllerOpt(GuiId.chooseDiffBar, { mode: mode, difficult: difficult }), tempPlayer, OptType.reflush);
                        }
                    )
                    break;
                }
                case SyncId.chooseArchive: {
                    const archiveIndex = param.archiveIndex;
                    triggerPlayer.data[PlayerDataKey.archiveIndex] = archiveIndex;
                    GuiController.chooseArchiveFun(triggerPlayer);
                    GuiController.getInstance().showGui(new GuiControllerOpt(GuiId.chooseArchiveBar, {}), triggerPlayer, OptType.close);
                    break;
                }
                case SyncId.loseArchive: {
                    const archiveIndex = param.archiveIndex;
                    const request: BroadcastRequest = { broadcastType: BroadcastType.clearUnitStore, controllerNo: ControllerNo.StoreController, data: { player: triggerPlayer, archiveIndex: archiveIndex } }
                    ControllerBroadcast.getInstance().broadcast(request);
                    break;
                }
                case SyncId.consumeExp: {
                    const exp = param.exp;
                    const handBookId = param.handBookId;
                    AchievementController.getInstance().consumeExp(triggerPlayer, exp, handBookId);
                    GuiController.getInstance().showGui(new GuiControllerOpt(GuiId.handBookBar, {}), triggerPlayer, OptType.reflush);
                    break;
                }
                case SyncId.useIntegral: {
                    const achId = param.achId;
                    const integral = param.integral;
                    const ret = AchievementController.getInstance().buyAch(triggerPlayer, integral, achId);
                    if (ret) {
                        GuiController.getInstance().showGui(new GuiControllerOpt(GuiId.integralMallBar, {}), triggerPlayer, OptType.reflush);
                    }
                    break;
                }
                case SyncId.doActionByStr: {
                    const name = param.name;
                    const value = param.value;
                    InputUtil.doActionByStr(triggerPlayer, name, value)
                    break;
                }
                case SyncId.buyItem: {
                    const itemId = param.itemId;
                    const sellUnitId = param.sellUnitId;
                    const buyUnitId = param.buyUnitId;
                    GoodController.getInstance().buyItem(itemId, buyUnitId, sellUnitId);
                    break;
                }
                case SyncId.sellItem: {
                    const index = param.index;
                    GoodController.getInstance().sellGoods(index, triggerPlayer);
                    break;
                }
                case SyncId.washItem: {
                    const index = param.index;
                    GoodController.getInstance().washGoods(index, triggerPlayer);
                    break;
                }
                case SyncId.decomposeItem: {
                    const index = param.index;
                    GoodController.getInstance().decomposeGoods(index, triggerPlayer);
                    break;
                }
                case SyncId.forgItem: {
                    const index = param.index;
                    GoodController.getInstance().forgGoods(index, triggerPlayer);
                    break;
                }
                case SyncId.dropItem: {
                    const index = param.index;
                    GoodController.getInstance().dropGoods(index, triggerPlayer);
                    break;
                }
                case SyncId.useItem: {
                    const index = param.index;
                    GoodController.getInstance().useGoods(index, triggerPlayer);
                    break;
                }
                case SyncId.unUseItem: {
                    const index = param.index;
                    GoodController.getInstance().unUseGoods(index, triggerPlayer);
                    break;
                }
                case SyncId.tidyUpItems: {
                    GoodController.getInstance().tidyUpItems(triggerPlayer);
                    break;
                }
                case SyncId.setTargetItemByIndex: {
                    const index = param.index;
                    const unit = triggerPlayer.getLastSelectedUnit();
                    if (unit) {
                        let tempItem: ItemModel;
                        unit.mapItems(
                            (item, tempIndex) => {
                                if (index === tempIndex) {
                                    tempItem = item;
                                    return true
                                }
                            }
                        )
                        if (tempItem) {
                            triggerPlayer.data[PlayerDataKey.targetItem] = tempItem;
                        }
                    }
                    break;
                }
                case SyncId.moveTargetItemIndex: {
                    const index = param.index;
                    GuiController.getInstance().changeItemIndexOrUse(triggerPlayer, index);
                    break;
                }
                case SyncId.useItemByIndex: {
                    const index = param.index;
                    const unit = triggerPlayer.getLastSelectedUnit();
                    if (unit) {
                        let tempItem
                        unit.mapItems(
                            (item, tempIndex) => {
                                if (index === tempIndex) {
                                    tempItem = item;
                                    return true;
                                }
                            }
                        )
                        if (tempItem) {
                            TriggerFactory.getInstance().executeTrigger(TriggerKey.useItem, unit, tempItem, triggerPlayer)
                        }
                    }

                    break;
                }
                case SyncId.useAbility: {
                    const key = param.key;
                    GuiController.getInstance().checkUseAbility(triggerPlayer, key)
                }
                // case SyncId.setTargetEffect: {
                //     const id = param.id;
                //     const spellInfo = triggerPlayer.data[PlayerDataKey.targetSpellInfo];
                //     spellInfo.id = id;
                //     break;
                // }
            }
        })

        // const mouseWheelFun = function (this: void, player: PlayerModel, wheelNum: number) {
        //     Logger.toastError(`滚动  ${wheelNum}`)
        // }
        // TriggerFactory.getInstance().registerMouseWheel(mouseWheelFun);

        // PlayerFactory.getInstance().mapAllPlayingPlayer((player) => {
        //     const playerId = player.playerId;
        //     this.guiEventMap[playerId] = {};
        // })

        this.guiSourceMap.initFun();

        PlayerFactory.getInstance().startCheckSync();
    }
    loadByJson(json: SerializeBody, player: PlayerModel) {
    }
    closeGui = (guiId: GuiId, player: PlayerModel) => {
        if (this.guiSourceMap[guiId]) {
            const visible = this.guiSourceMap[guiId].getVisible();
            if (visible) {
                const tempData = { };
                const opt = new GuiControllerOpt(guiId, tempData);
                this.showGui(opt, player, OptType.close);
            }
        }
    }
    // 默认通过背景计时器去执行ui动作防抖，bForce为ture时为强制立即执行
    showGui = (opt: GuiControllerOpt, player: PlayerModel, optType: OptType = OptType.open) => {
        const id: GuiId = opt.id as GuiId;
        if (this.guiSourceMap[id]) {
        } else {
            return;
        }
        if (player?.isLocalPlayer()) {
        } else {
            return;
        }
        GlobalModel.bAsync = true;
        let visible = false;
        switch (optType) {
            case OptType.open:{
                visible = true;
                break;
            }
            case OptType.close:{
                visible = false;
                break;
            }
            case OptType.reverse:{
                visible = !this.guiSourceMap[id].getVisible();
                break;
            }
            case OptType.reflush:{
                visible = this.guiSourceMap[id].getVisible();
                break;
            }
        }
        const fun = () => {
            const data: any = opt.request;
            switch (id) {
                case GuiId.goodsBar: {
                    if (visible) {
                        this.guiSourceMap[GuiId.goodsBar].reflushGoodsBar();
                    } else {
                        this.guiSourceMap[GuiId.goodsBar].close();
                    }
                    break;
                }
                case GuiId.equipBar: {
                    const selectUnit = player.data[PlayerDataKey.selectedUnit];
                    const owner = PlayerFactory.getInstance().getUnitOwner(selectUnit);
                    const hero = GlobalEnv.getHero(owner);

                    if (selectUnit === hero) {
                        if (owner?.isValid()) {
                            const isPlaying = owner.isNotEndPlaying();
                            if (isPlaying) {
                                this.guiSourceMap[GuiId.equipBar].reflushEquipBar(owner);
                                break;
                            }
                        }
                    }
                    this.guiSourceMap[GuiId.equipBar].close();
                    break;
                }
                case GuiId.chooseItemBar: {
                    if (visible) {
                        const id = data.itemId;
                        const ownerId = data.ownerId;
                        this.guiSourceMap[GuiId.chooseItemBar].addChooseTask(id, ownerId);
                    } else {
                        this.guiSourceMap[GuiId.chooseItemBar].toNextTask();
                    }
                    break;
                }

                case GuiId.achBar: {
                    if (visible) {
                        this.guiSourceMap[GuiId.achBar].openBar();
                    } else {
                        this.guiSourceMap[GuiId.achBar].closeBar()
                    }
                    break;
                }
                case GuiId.hideAchBar: {
                    if (visible) {
                        this.guiSourceMap[GuiId.hideAchBar].openBar();
                    } else {
                        this.guiSourceMap[GuiId.hideAchBar].closeBar()
                    }
                    break;
                }
                case GuiId.talentBar: {
                    if (visible) {
                        this.guiSourceMap[GuiId.talentBar].openBar();
                    } else {
                        this.guiSourceMap[GuiId.talentBar].closeBar()
                    }
                    break;
                }
                case GuiId.attrBar: {
                    if (visible) {
                        this.guiSourceMap[GuiId.attrBar].openBar();
                    } else {
                        this.guiSourceMap[GuiId.attrBar].closeBar()
                    }
                    break;
                }
                case GuiId.appBar: {
                    if (visible) {
                        this.guiSourceMap[GuiId.appBar].openBar();
                    } else {
                        this.guiSourceMap[GuiId.appBar].closeBar()
                    }
                    break;
                }
                case GuiId.integralMallBar: {
                    if (visible) {
                        this.guiSourceMap[GuiId.integralMallBar].openBar();
                    } else {
                        this.guiSourceMap[GuiId.integralMallBar].closeBar()
                    }
                    break;
                }
                case GuiId.handBookBar: {
                    if (visible) {
                        this.guiSourceMap[GuiId.handBookBar].openBar();
                    } else {
                        this.guiSourceMap[GuiId.handBookBar].closeBar()
                    }
                    break;
                }

                case GuiId.textGuiBar: {
                    const uiName = data.uiName;
                    const index = this.guiSourceMap[GuiId.textGuiBar].getIndexByUiName(uiName);
                    const size = data.size;
                    const pos = data.pos;
                    const path = data.path;
                    const autoClose = data.autoClose;
                    this.guiSourceMap[GuiId.textGuiBar].showTextGui(visible, index, uiName, size, pos, path, autoClose);
                    break;
                }
                case GuiId.toastWheelBar: {
                    // const path = data.path;
                    this.guiSourceMap[GuiId.toastWheelBar].openBar();
                    break;
                }
                case GuiId.taskButton: {
                    this.guiSourceMap[GuiId.taskButton].reflushTask();
                    break;
                }
                case GuiId.chooseHeroBar: {
                    if (visible) {
                        const unitPoolDatas = data.unitPoolDatas;
                        const autoShowAllHero = Configer.getConfig(ConfigKey.autoShowAllHero);
                        this.guiSourceMap[GuiId.chooseHeroBar].showHeroBar(unitPoolDatas, (autoShowAllHero > 0), visible)
                    } else {
                        this.guiSourceMap[GuiId.chooseHeroBar].closeChooseHero();
                    }
                    break;
                }
                case GuiId.chooseAchBar: {
                    if (visible) {
                        this.guiSourceMap[GuiId.chooseAchBar].addChooseTask('', '')
                    } else {
                        this.guiSourceMap[GuiId.chooseAchBar].toNextTask();
                    }
                    break;
                }
                case GuiId.damageButton: {
                    this.guiSourceMap[GuiId.damageButton].reflushHurt()
                    break;
                }
                case GuiId.bossLifeBar: {
                    let maxLife = 0;
                    let life = 0;
                    const shareBossArr = GlobalEnv.shareBossArr;
                    const shareBossLen = shareBossArr.length;

                    const boss = player.data[PlayerDataKey.lastHurtBoss];
                    if (shareBossLen > 0) {
                        maxLife = GlobalEnv.shareBossMaxLife;
                        life = GlobalEnv.shareBossLife;
                    } else if (boss) {
                        maxLife = boss.maxLife;
                        life = boss.life;
                    }

                    if (maxLife > 0) {
                        this.guiSourceMap[GuiId.bossLifeBar].reflushBar(life, maxLife)
                    } else {
                        this.guiSourceMap[GuiId.bossLifeBar].canel()
                    }
                    break;
                }
                case GuiId.combinationBar: {
                    this.guiSourceMap[GuiId.combinationBar].reflushCombinationInfo()
                    break;
                }
                case GuiId.petBar: {
                    // 走按钮的逻辑
                    if (visible) {
                        this.guiSourceMap[GuiId.petBar].showPetBar()
                    } else {
                        this.guiSourceMap[GuiId.petBar].closePetBar()
                    }
                    break;
                }
                case GuiId.resumeBar: {
                    // 走按钮的逻辑
                    if (visible) {
                        const cost = UnitController.getInstance().getUnitCost(player)
                        this.guiSourceMap[GuiId.resumeBar].showResumeBar(cost)
                    } else {
                        this.guiSourceMap[GuiId.resumeBar].canel()
                    }
                    break;
                }
                case GuiId.buffBar: {
                    if (visible) {
                        this.guiSourceMap[GuiId.buffBar].resetBuffs();
                    } else {
                        this.guiSourceMap[GuiId.buffBar].closeBar();
                    }
                    break;
                }
                case GuiId.chooseDiffBar: {
                    // 走按钮的逻辑
                    if (visible) {
                        if (data.currentDiff) {
                            const currentDiff = data.currentDiff;
                            this.guiSourceMap[GuiId.chooseDiffBar].openBar(currentDiff);
                        } else {
                            const difficult = data.difficult;
                            const mode = data.mode;
                            this.guiSourceMap[GuiId.chooseDiffBar].reflush(mode, difficult);
                        }
                    } else {
                        this.guiSourceMap[GuiId.chooseDiffBar].closeBar();
                    }
                    break;
                }
                case GuiId.adminBar: {
                    if (visible) {
                        this.guiSourceMap[GuiId.adminBar].openBar();
                    } else {
                        this.guiSourceMap[GuiId.adminBar].closeBar()
                    }
                    break;
                }
                case GuiId.multiBoradBar: {
                    if (visible) {
                        this.guiSourceMap[GuiId.multiBoradBar].openBar();
                    }
                    break;
                }
                case GuiId.smallAttrBar: {
                    if (visible) {
                        this.guiSourceMap[GuiId.smallAttrBar].openBar();
                    }
                    break;
                }
                case GuiId.avatarBar: {
                    if (visible) {
                        this.guiSourceMap[GuiId.avatarBar].openBar();
                    }
                    break;
                }
                case GuiId.skillBar: {
                    if (visible) {
                        this.guiSourceMap[GuiId.skillBar].openBar();
                    }
                    break;
                }
                case GuiId.itemBar: {
                    if (visible) {
                        this.guiSourceMap[GuiId.itemBar].openBar();
                    }
                    break;
                }
                case GuiId.chooseArchiveBar: {
                    if (visible) {
                        const unitJsons = data.unitJsons;
                        this.guiSourceMap[GuiId.chooseArchiveBar].openBar(unitJsons);
                    } else {
                        this.guiSourceMap[GuiId.chooseArchiveBar].closeBar();
                    }
                    break;
                }
                case GuiId.unitShopBar: {
                    this.guiSourceMap[GuiId.unitShopBar].openBar();
                    break;
                }
                case GuiId.topBar: {
                    this.guiSourceMap[GuiId.topBar].openBar();
                    break;
                }
                case GuiId.chatBar: {
                    this.guiSourceMap[GuiId.chatBar].openBar();
                    break;
                }
            }
        }
        fun();
        GlobalModel.bAsync = false;
    }
    // 这个方法只在对应ui处于打开状态时生效
    notify(request: BroadcastRequest): BroadcastResponse {
        const ret: BroadcastResponse = { controllerNo: this.getControllerNo(), data: {}, status: BroadcastResponseStatus.success };
        const type = request.broadcastType;
        const data = request.data;
        switch (type) {
            case BroadcastType.showUi: {
                const guiId = data.guiId;
                const player = data.player;
                const upDateData = data.data;
                const opt = new GuiControllerOpt(guiId, upDateData);
                GuiController.getInstance().showGui(opt, player, OptType.open);
                break;
            }
            case BroadcastType.updateGui: {
                const guiId = data.guiId;
                const player = data.player;
                const upDateData = data.data;
                const opt = new GuiControllerOpt(guiId, upDateData);
                GuiController.getInstance().showGui(opt, player, OptType.reflush);
                break;
            }
            case BroadcastType.showTextUi: {
                const uiName = data.uiName;
                const size = data.size;
                const pos = data.pos;
                const autoClose = data.autoClose;
                const path = data.path;
                const player = data.player;
                this.showUiText(uiName, autoClose, player, path, size, pos);
                break;
            }
            default :
                ret.status = BroadcastResponseStatus.failed;
                break;
        }
        return ret;
    }
    showUiText(uiName: ResourceKey, autoClose: boolean = true, player?: PlayerModel, path?: string, size?: { h: number, w: number }, pos?: { x: number, y: number }) {
        const openOpt = new GuiControllerOpt(GuiId.textGuiBar, { uiName: uiName, size: size, pos: pos, autoClose: autoClose, path: path });
        if (player) {
            this.showGui(openOpt, player, OptType.open);
        } else {
            PlayerFactory.getInstance().mapAllNotEndPlayingPlayer(
                (player) => {
                    this.showGui(openOpt, player, OptType.open);
                }
            )
        }
        if (autoClose) {
            const closeOpt = new GuiControllerOpt(GuiId.textGuiBar, { uiName: uiName, visible: false });
            const endCallBack = () => {
                if (player) {
                    this.showGui(closeOpt, player, OptType.close);
                } else {
                    PlayerFactory.getInstance().mapAllNotEndPlayingPlayer(
                        (player) => {
                            this.showGui(closeOpt, player, OptType.close);
                        }
                    )
                }
            }
            const textGuiInterval: number = Configer.getUIConfig(UIConfigKey.TextGuiInterval);
            const existenceOpt = new TimeControllerOpt(undefined, endCallBack, 1);
            TimeController.getInstance().startTimer({time: textGuiInterval, opt: existenceOpt});
        }
    }

    escOtherFun = (player: PlayerModel, guiId: GuiId) => {
        const guiIdMapArr: GuiId[] = Configer.getUIConfig(UIConfigKey.guiIdCloseArr);
        for (let i = 0; i < guiIdMapArr.length; i++) {
            const tempGuiId = guiIdMapArr[i];
            if (guiId === tempGuiId) {
                continue;
            }
            GuiController.getInstance().closeGui(tempGuiId, player)
        }
    }

    escFun = (player: PlayerModel) => {
        const guiIdMapArr: GuiId[] = Configer.getUIConfig(UIConfigKey.guiIdCloseArr);
        for (let i = 0; i < guiIdMapArr.length; i++) {
            const guiId = guiIdMapArr[i];
            GuiController.getInstance().closeGui(guiId, player)
        }
    }

    serialize(): SerializeBody {
        return {};
    }


    getMultiBoradRow(player: PlayerModel) {
        const hero = GlobalEnv.getHero(player);
        if (hero) {
        } else {
            const multiBoradRow: MultiBoradRow = {
                heroLevel: '',
                // heroName: '',
                userName: '',
                fightValue: '0',
                // integral: '0',
            }
            return multiBoradRow
        }
        const heroLevel = hero.level;
        const heroName = hero.name;
        let userName = '已失败';
        if (player.isNotEndPlaying()) {
            userName = player.name;
        }
        const attr = UnitUtil.getAttribute(hero);
        let fightValue = UnitUtil.getFightValueByAttr(attr.newestAttribute);
        let integral = AchievementController.getInstance().getIntegral(player);
        // const playerId = GetPlayerId(player);
        // const hurtNum = GlobalEnv.hurtNum[playerId];
        const multiBoradRow: MultiBoradRow = {
            heroLevel: `${heroLevel}`,
            // heroName,
            userName,
            fightValue: `${fightValue}`,
            // integral: `${integral}`,
        }
        return multiBoradRow;
    }

    getItemShopInfos(unit: UnitModel, player: PlayerModel) {
        const itemShopInfos: ItemShopInfo[] = [];
        if (unit) {
            const items = unit.template.Sellitems;
            const itemArr = CodeUtil.getArrBySplit(items);
            for (let i = 0; i < itemArr.length; i++) {
                const itemId = itemArr[i];
                if (!CodeUtil.isNullOrZero(itemId)) {
                    const itemShopMap = player.data[PlayerDataKey.itemShopMap];
                    if (itemShopMap) {
                        itemShopInfos.push(itemShopMap[itemId]);
                    }
                }
            }
        }
        return itemShopInfos
    }

    checkUseAbility (player: PlayerModel, key: string) {
        const bar = this.guiSourceMap[GuiId.skillBar];
        if (bar) {
        } else {
            return;
        }
        const selectedUnit = player.getLastSelectedUnit();
        if (selectedUnit) {
            const abilityJson = AbilityController.getInstance().getAbilityJsonByIndex(selectedUnit, key);
            const abilityData = DataRouter.getAbilityDataById(abilityJson?.id);
            if (abilityJson?.cd) {
                if (abilityJson.cd > 0) {
                    Logger.toastProduct('技能正在冷却!', player);
                    return;
                } else if (abilityJson.cd < 0) {
                    Logger.toastProduct('被动技能无法主动使用!', player);
                    return;
                }
            } else {
                Logger.toastProduct('使用失败，当前位置没有技能!', player);
                return;
            }
            if (abilityData) {
                const requireAch = abilityData.requiredAchs;
                const cost = abilityData.Cost;
                const achArr = CodeUtil.getArrBySplit(requireAch);
                const ret = AchievementController.getInstance().checkHasAchs(player, achArr, 1);
                if (ret) {
                } else {
                    Logger.toastProduct('技能所需条件不满足!', player);
                    return;
                }
                const newesMana = selectedUnit.mana;
                if (!CodeUtil.isNullOrZero(cost)) {
                    if (newesMana && newesMana > cost) {
                    } else {
                        Logger.toastProduct('法力值不足!', player);
                        return;
                    }
                }

                const castType = abilityData.cast_type1;

                switch (castType) {
                    case CastType1.null: {
                        selectedUnit.useAbility(abilityJson.id)
                        break;
                    }
                    case CastType1.point:
                    case CastType1.unit: {
                        player.showAbilityEffect(abilityJson, castType)
                        break;
                    }
                }
            } else {
                Logger.toastError('当前快捷键没有对应技能!');
                return;
            }
        }
    }

    changeItemIndexOrUse (activePlayer: PlayerModel, index: number) {
        const unit = activePlayer.getLastSelectedUnit();
        if (unit?.isValid()) {
            const targetItem = activePlayer.data[PlayerDataKey.targetItem];
            if (targetItem) {
                let useId;
                let upQualityPro = 0;
                let downQualityPro = 0;

                let tempItem: ItemModel;
                unit.mapItems((item, tempIndex) => {
                    if (index === tempIndex) {
                        tempItem = item;
                        return true;
                    }
                })
                if (tempItem) {
                    const strId = targetItem.strId;
                    if (strId === Configer.getFinal(FinalKey.itemLevelUpItem)) {
                        const itemData = tempItem.template;
                        upQualityPro = itemData.upSuccess;
                        downQualityPro = itemData.downSuccess;
                        useId = strId;
                    } else if (strId === Configer.getFinal(FinalKey.itemLevelUpItemSuper)) {
                        upQualityPro = 100;
                        downQualityPro = 0;
                        useId = strId;
                    }
                }

                if (useId) {
                    ModelFactory.getInstance().consumItemOnUnit(unit, useId, false, 1);
                    const ret = GoodController.getInstance().itemLevelUp(unit, activePlayer, tempItem, upQualityPro, downQualityPro, useId);
                    if (ret) {
                        activePlayer.data[PlayerDataKey.targetItem] = undefined;
                        const opt = new GuiControllerOpt(GuiId.goodsBar, {});
                        GuiController.getInstance().showGui(opt, activePlayer, OptType.reflush);
                    }
                } else {
                    ModelFactory.getInstance().unitAddItem(unit, targetItem, index);
                    activePlayer.data[PlayerDataKey.targetItem] = undefined;
                    GoodController.getInstance().reflushItemBag(unit);
                }
            }
        }
    }

}